<template>
  <el-card class="box-card">
    <!-- 搜索框和新增按钮 -->
    <el-row :gutter="20" style="margin-bottom: 20px;">
      <el-col :span="6">
        <el-input
          v-model="searchKeyword"
          placeholder="请输入姓名、站点或邮箱"
          size="mini"
          clearable
          @keyup.enter="handleSearch"
        >
          <template #append>
            <el-button icon="el-icon-search" @click="handleSearch" />
          </template>
        </el-input>
      </el-col>
      <el-col :span="1">
        <el-button size="mini" icon="el-icon-circle-plus-outline" type="primary" @click="add">
          寄快递
        </el-button>
      </el-col>
    </el-row>

    <!-- Dialog 对话框 弹出新增和修改表单 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="id:" hidden>
          <el-input v-model="form.id" style="text-align: left;"></el-input>
        </el-form-item>
        <el-form-item label="寄件人姓名:" prop="sendername">
          <el-input v-model="form.sendername" placeholder="请输入姓名" style="width:80%; text-align: left;"></el-input>
        </el-form-item>
        <el-form-item label="寄件人电话:" prop="senderphone">
          <el-input v-model="form.senderphone" placeholder="请输入电话" style="width:80%; text-align: left;"></el-input>
        </el-form-item>
        <el-form-item label="地点:" prop="space">
          <el-input v-model="form.space" placeholder="请输入邮箱" style="width:80%; text-align: left;"></el-input>
        </el-form-item>
        <el-form-item label="收件人姓名:" prop="addressee">
          <el-input v-model="form.addressee" placeholder="请输入姓名" style="width:80%; text-align: left;"></el-input>
        </el-form-item>
        <el-form-item label="收件人电话:" prop="addresseephone">
          <el-input v-model="form.addresseephone" placeholder="请输入电话" style="width:80%; text-align: left;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">提 交</el-button>
      </div>
    </el-dialog>

    <!-- 表格 -->
    <el-table ref="singleTable" :data="filteredTableData" style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column property="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column property="sendername" label="寄件人姓名" width="120" align="center"></el-table-column>
      <el-table-column property="senderphone" label="寄件人电话" width="120" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.senderphone ? '******' : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column property="space" label="地点" width="120" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.space ? '******' : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column property="addressee" label="收件人姓名" width="120" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.addressee ? '******' : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column property="addresseephone" label="收件人电话" width="120" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.addresseephone ? '******' : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column property="zhandianaccept" label="站点接受" width="120" align="center"></el-table-column>
      <el-table-column property="cheduiaccept" label="车队接受" width="120" align="center"></el-table-column>
      <el-table-column property="state" label="状态"  align="center"></el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="margin-top: 20px; text-align: center;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </el-card>
</template>

<script>
export default {
  name: "parcel",
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      title: '',
      dialogFormVisible: false,
      form: {
        id: null,
        sendername: null,
        senderphpne: null,
        space: null,
        addressee: null,
        addresseephone: null,
        cheduiaccept: null,
        zhandianaccept: null,
        state:null
      },
      tableData: [], // 表格数据
      searchKeyword: '', // 搜索关键词
      rules: {
        sendername: [{ required: true, message: '请输入寄件人姓名', trigger: 'blur' }],
        senderphone: [{ required: true, message: '请输入寄件人电话', trigger: 'blur' }],
        space: [{ required: true, message: '请输入地点', trigger: 'blur' }],
        addressee: [{ required: true, message: '请输入收件人姓名', trigger: 'blur' }],
        addresseephone: [{ required: true, message: '请输入收件人电话', trigger: 'blur' }],
        cheduiaccept: [{ required: true, message: '请输入车队接收', trigger: 'blur' }],
        zhandianaccept: [{ required: true, message: '请输入站点接收', trigger: 'blur' }],
        state: [{ required: true, message: '请输入状态', trigger: 'blur' }]
      }
    };
  },
  computed: {
    filteredTableData() {
      let filteredData = this.tableData;

      // 搜索过滤
      if (this.searchKeyword) {
        const keyword = this.searchKeyword.toLowerCase();
        filteredData = this.tableData.filter(
          (item) =>
            (item.id && item.id.toString().toLowerCase().includes(keyword)) ||
            (item.sendername && item.sendername.toLowerCase().includes(keyword))
        );
      }

      // 更新总数（防止 total 过时）
      this.total = filteredData.length;

      // 计算分页数据
      const startIndex = (this.currentPage - 1) * this.pageSize;
      const endIndex = startIndex + this.pageSize;
      return filteredData.slice(startIndex, endIndex);
    }
  },
  methods: {
    // 表单重置初始化
    reset() {
      this.form = {
        id: null,
        sendername: null,
        senderphpne: null,
        space: null,
        addressee: null,
        addresseephone: null,
        cheduiaccept: null,
        zhandianaccept: null,
        state: '未揽收'
      };
    },



    // 新增
    add() {
      this.reset();
      this.dialogFormVisible = true;
      this.title = "新增数据";
    },

    // 删除
    remove(index, row) {
      this.$axios({
        method: 'post',
        url: 'http://localhost:9090/parcel/remove/' + row.id,
      }).then((response) => {
        this.$message({
          message: '删除成功!',
          type: 'success'
        });
        this.getList();
      }).catch((error) => {
      });
    },

    // 编辑
    edit(index, row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
      this.title = "修改数据";
    },

    // 查询
    getList() {
      this.$axios({
        method: 'get',
        url: 'http://localhost:9090/parcel/info',
      }).then((response) => {
        this.tableData = response.data;
        this.total = response.data.length; // 计算总条数
        this.currentPage = 1; // 重新获取数据后，默认回到第一页
      }).catch((error) => {
        console.error(error);
      });
    },

    // 提交
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const url = this.form.id ? 'http://localhost:9090/parcel/edit' : 'http://localhost:9090/parcel/add';
          this.$axios({
            method: 'post',
            data: this.form,
            url: url,
          }).then((response) => {
            this.$message({
              message: this.form.id ? '修改成功!' : '新增成功!',
              type: 'success'
            });
            this.dialogFormVisible = false;
            this.getList();
          }).catch((error) => {
          });
        } else {
          return false;
        }
      });
    },

    // 搜索
    handleSearch() {
      // 搜索逻辑已在 computed 中实现
    },

    // 分页大小改变
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1; // 切换分页大小时，回到第一页
    },

    // 当前页改变
    handleCurrentChange(val) {
      this.currentPage = val;
    }
  },
  mounted() {
    this.getList();
  }
};
</script>

<style scoped>
.box-card {
  margin: 20px;
}
</style>
